﻿
namespace NetROS.Tiers.Windows.Forms
{
	/// <summary>
	/// abstract Parametri typed datagridview
	/// </summary>
	[System.ComponentModel.DesignerCategoryAttribute("code")]
	public abstract class ParametriDataGridViewBase : System.Windows.Forms.UserControl
	{	
		/// <summary> 
		/// Thrown when the current Entity changed.
		/// </summary>
		public event System.EventHandler<ParametriDataGridViewEventArgs> CurrentEntityChanged;
		
		#region Fields
				
		/// <summary> 
		/// the current entity.
		/// </summary>
		private Entities.Parametri _currentParametri = null;
		//private Dictionary<string, string> _columnsHeaders = new Dictionary<string, string>();
		
		/// <summary> 
		/// the datagridview instance
		/// </summary>
		protected System.Windows.Forms.DataGridView uxParametriDataGridView;
		
		/// <summary> 
		/// the errorprovider
		/// </summary>
		protected System.Windows.Forms.ErrorProvider uxParametriErrorProvider;
		
		/// <summary> 
		/// the bindingsource
		/// </summary>
		protected System.Windows.Forms.BindingSource uxParametriBindingSource;
		
		/// <summary> 
		/// the DGV column associated with the IDGrupaParametra property
		/// </summary>
		protected System.Windows.Forms.DataGridViewComboBoxColumn uxIDGrupaParametraDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the Param property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxParamDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the Vrijednost property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxVrijednostDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the ID property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxIDDataGridViewColumn;
		
		#endregion
					
		#region Combo list
		
				
		private Entities.TList<Entities.GrupaParametra> _IDGrupaParametraList;
		
		/// <summary> 
		/// The list of selectable GrupaParametra
		/// </summary>
		[System.ComponentModel.Category("DropDownList BindingSources")]
		public Entities.TList<Entities.GrupaParametra> IDGrupaParametraList
		{
			get {return this._IDGrupaParametraList;}
			set 
			{
				this._IDGrupaParametraList = value;
				this.uxIDGrupaParametraDataGridViewColumn.DataSource = null;
				this.uxIDGrupaParametraDataGridViewColumn.DataSource = this._IDGrupaParametraList;
			}
		}
		
		private bool _allowNewItemInIDGrupaParametraList;
		
		/// <summary> 
		/// Indicates if user can add an item in the list of GrupaParametra
		/// </summary>
		[System.ComponentModel.Category(".netTiers"), System.ComponentModel.Description("Indicates if end user can add a new item in the combobox. an event will be thrown then.")]
		public bool AllowNewItemInIDGrupaParametraList
		{
			get { return _allowNewItemInIDGrupaParametraList;}
			set
			{
				this._allowNewItemInIDGrupaParametraList = value;
				this.uxIDGrupaParametraDataGridViewColumn.DisplayStyle = value ? System.Windows.Forms.DataGridViewComboBoxDisplayStyle.ComboBox : System.Windows.Forms.DataGridViewComboBoxDisplayStyle.DropDownButton;
			}
		}
		
		#endregion
		
		#region Main Datasource
		
		private Entities.TList<Entities.Parametri> _ParametriList;
				
		/// <summary> 
		/// The list of Parametri to be displayed in the DGV.
		/// </summary>
		[System.ComponentModel.Category("Data")]
		public Entities.TList<Entities.Parametri> ParametriList
		{
			get {return this._ParametriList;}
			set
			{
				this._ParametriList = value;
				this.uxParametriBindingSource.DataSource = null;
				this.uxParametriBindingSource.DataSource = value;
				this.uxParametriDataGridView.DataSource = null;
				this.uxParametriDataGridView.DataSource = this.uxParametriBindingSource;				
				//this.uxParametriBindingSource.ListChanged += new System.ComponentModel.ListChangedEventHandler(uxParametriBindingSource_ListChanged);
				this.uxParametriBindingSource.CurrentItemChanged += new System.EventHandler(OnParametriBindingSourceCurrentItemChanged);
			}
		}
		
		private void OnParametriBindingSourceCurrentItemChanged(object sender, System.EventArgs e)
		{
			_currentParametri = uxParametriBindingSource.Current as Entities.Parametri;
			
			if (_currentParametri != null)
			{
				_currentParametri.Validate();
			}
			//_Parametri.Validate();
			OnCurrentEntityChanged();
		}

		//void uxParametriBindingSource_ListChanged(object sender, System.ComponentModel.ListChangedEventArgs e)
		//{
			//throw new Exception("The method or operation is not implemented.");
		//}		
		
		#endregion
		
		/// <summary>
		/// Gets the currently selected <c cref="Entities.Parametri"/> instance.
		/// </summary>
		public Entities.Parametri SelectedParametri
		{
			get {return this._currentParametri;}
		}
		
		#region other properties
		
		/// <summary>
		/// Gets or sets a value indicating whether you have provided your own data-management operations for the DataGridView control.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool VirtualMode
		{
			get {return this.uxParametriDataGridView.VirtualMode;}
			set
			{
				this.uxParametriDataGridView.VirtualMode = value;
			}
		}
		
		/// <summary>
		/// Gets or sets a value indicating whether the option to add rows is displayed to the user.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool AllowUserToAddRows
		{
			get {return this.uxParametriDataGridView.AllowUserToAddRows;}
			set {this.uxParametriDataGridView.AllowUserToAddRows = value;}
		}
		
		/// <summary>
		/// Gets or sets a value indicating whether the user is allowed to delete rows from the DataGridView.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool AllowUserToDeleteRows
		{
			get {return this.uxParametriDataGridView.AllowUserToDeleteRows;}
			set {this.uxParametriDataGridView.AllowUserToDeleteRows = value;}
		}
		
		/// <summary>
		/// Gets the <c cref="DataGridView"/> instance.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public System.Windows.Forms.DataGridView DataGridView
		{
			get {return this.uxParametriDataGridView;}
		}
		
		/// <summary>
		/// Gets a collection that contains all the columns in the control.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]		
		[System.ComponentModel.DesignerSerializationVisibilityAttribute(System.ComponentModel.DesignerSerializationVisibility.Visible)]
		//[System.ComponentModel.EditorAttribute("System.Windows.Forms.Design.DataGridViewColumnCollectionEditor, System.Design", typeof(System.Drawing.Design.UITypeEditor))] 
		public System.Windows.Forms.DataGridViewColumnCollection Columns
		{
			get { return this.uxParametriDataGridView.Columns; }
		}
		
		#endregion
		
		/// <summary> 
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.IContainer components = null;
	
		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
		protected override void Dispose(bool disposing)
		{
			if (disposing && (components != null))
			{
				components.Dispose();
			}
			base.Dispose(disposing);
		}
	
		#region Constructor
	
		/// <summary>
		/// Initializes a new instance of the <see cref="ParametriDataGridViewBase"/> class.
		/// </summary>
		public ParametriDataGridViewBase()
		{			
			this.components = new System.ComponentModel.Container();			
			
			this.uxParametriDataGridView = new System.Windows.Forms.DataGridView();
			this.uxParametriBindingSource = new System.Windows.Forms.BindingSource(this.components);
			this.uxParametriErrorProvider = new System.Windows.Forms.ErrorProvider(this.components);
			this.uxIDGrupaParametraDataGridViewColumn = new System.Windows.Forms.DataGridViewComboBoxColumn();
			this.uxParamDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.uxVrijednostDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.uxIDDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			//this.uxIDGrupaParametraBindingSource = new GrupaParametraBindingSource(this.components);
			//((System.ComponentModel.ISupportInitialize)(this.uxIDGrupaParametraBindingSource)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxParametriDataGridView)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxParametriBindingSource)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxParametriErrorProvider)).BeginInit();
			this.SuspendLayout();
						
			// 
			// uxParametriErrorProvider
			// 
			this.uxParametriErrorProvider.ContainerControl = this;
			this.uxParametriErrorProvider.DataSource = this.uxParametriBindingSource;						
			// 
			// uxParametriDataGridView
			// 
			this.uxParametriDataGridView.AutoGenerateColumns = false;
			this.uxParametriDataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
			this.uxParametriDataGridView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
		this.uxIDGrupaParametraDataGridViewColumn,
		this.uxParamDataGridViewColumn,
		this.uxVrijednostDataGridViewColumn,
		this.uxIDDataGridViewColumn			});
			this.uxParametriDataGridView.Dock = System.Windows.Forms.DockStyle.Fill;
			this.uxParametriDataGridView.Location = new System.Drawing.Point(0, 0);
			this.uxParametriDataGridView.Name = "uxParametriDataGridView";
			this.uxParametriDataGridView.Size = new System.Drawing.Size(470, 300);
			this.uxParametriDataGridView.TabIndex = 0;	
			this.uxParametriDataGridView.BackgroundColor = System.Drawing.Color.WhiteSmoke;
			this.uxParametriDataGridView.EnableHeadersVisualStyles = false;
			this.uxParametriDataGridView.DataError += new System.Windows.Forms.DataGridViewDataErrorEventHandler(this.OnParametriDataGridViewDataError);
			this.uxParametriDataGridView.CellValueNeeded += new System.Windows.Forms.DataGridViewCellValueEventHandler(this.OnParametriDataGridViewCellValueNeeded);
			this.uxParametriDataGridView.CellValuePushed += new System.Windows.Forms.DataGridViewCellValueEventHandler(this.OnParametriDataGridViewCellValuePushed);
			
			//
			// uxIDGrupaParametraDataGridViewColumn
			//
			this.uxIDGrupaParametraDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxIDGrupaParametraDataGridViewColumn.DataPropertyName = "IDGrupaParametra";
			this.uxIDGrupaParametraDataGridViewColumn.HeaderText = "IDGrupaParametra";
			this.uxIDGrupaParametraDataGridViewColumn.Name = "uxIDGrupaParametraDataGridViewColumn";
			this.uxIDGrupaParametraDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxIDGrupaParametraDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxIDGrupaParametraDataGridViewColumn.ReadOnly = false;		
			//
			// uxParamDataGridViewColumn
			//
			this.uxParamDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxParamDataGridViewColumn.DataPropertyName = "Param";
			this.uxParamDataGridViewColumn.HeaderText = "Param";
			this.uxParamDataGridViewColumn.Name = "uxParamDataGridViewColumn";
			this.uxParamDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxParamDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxParamDataGridViewColumn.ReadOnly = false;		
			//
			// uxVrijednostDataGridViewColumn
			//
			this.uxVrijednostDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxVrijednostDataGridViewColumn.DataPropertyName = "Vrijednost";
			this.uxVrijednostDataGridViewColumn.HeaderText = "Vrijednost";
			this.uxVrijednostDataGridViewColumn.Name = "uxVrijednostDataGridViewColumn";
			this.uxVrijednostDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxVrijednostDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxVrijednostDataGridViewColumn.ReadOnly = false;		
			//
			// uxIDDataGridViewColumn
			//
			this.uxIDDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxIDDataGridViewColumn.DataPropertyName = "ID";
			this.uxIDDataGridViewColumn.HeaderText = "ID";
			this.uxIDDataGridViewColumn.Name = "uxIDDataGridViewColumn";
			this.uxIDDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxIDDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxIDDataGridViewColumn.ReadOnly = true;		
			//
			// uxIDGrupaParametraDataGridViewColumn
			//				
			this.uxIDGrupaParametraDataGridViewColumn.DisplayMember = "Naziv";	
			this.uxIDGrupaParametraDataGridViewColumn.ValueMember = "IDGrupaParametra";	
			this.uxIDGrupaParametraDataGridViewColumn.DisplayStyleForCurrentCellOnly = true;
			//uxIDGrupaParametraDataGridViewColumn.DataSource = uxIDGrupaParametraBindingSource;				
				
			
			// this
			this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
			this.Controls.Add(this.uxParametriDataGridView);
			this.Name = "ParametriDataGridView";
			this.Size = new System.Drawing.Size(470, 300);
			//((System.ComponentModel.ISupportInitialize)(this.uxIDGrupaParametraBindingSource)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxParametriErrorProvider)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxParametriDataGridView)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxParametriBindingSource)).EndInit();
			this.ResumeLayout(false);
		}
		#endregion
				
		#region events
		
		/// <summary>
		/// Raised the CurrentEntityChanged event.
		/// </summary>
		protected void OnCurrentEntityChanged()
		{
			if (CurrentEntityChanged != null)
			{
				ParametriDataGridViewEventArgs args = new ParametriDataGridViewEventArgs();
				args.Parametri = _currentParametri;
				CurrentEntityChanged(this, args); 
			}
		}
		
		#endregion
		
		/// <summary>
		/// Strongly typed event 
		/// </summary>
		public class ParametriDataGridViewEventArgs : System.EventArgs
		{
			private Entities.Parametri	_Parametri;
	
			/// <summary>
			/// the  Entities.Parametri instance.
			/// </summary>
			public Entities.Parametri Parametri
			{
				get { return _Parametri; }
				set { _Parametri = value; }
			}	
		}
		
		/// <summary>
		/// Handles the DataError event of the uxParametriDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewDataErrorEventArgs"/> instance containing the event data.</param>
		protected void OnParametriDataGridViewDataError(object sender, System.Windows.Forms.DataGridViewDataErrorEventArgs e)
		{
			System.Diagnostics.Debug.WriteLine(string.Format( "{0}, {1}", e.ColumnIndex, e.RowIndex));
		}
		
		/// <summary>
		/// Handles the CellValueNeeded event of the uxParametriDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewCellValueEventArgs"/> instance containing the event data.</param>
		protected void OnParametriDataGridViewCellValueNeeded(object sender, System.Windows.Forms.DataGridViewCellValueEventArgs e)
		{
			switch(uxParametriDataGridView.Columns[e.ColumnIndex].Name)
			{
					case "uxIDGrupaParametraDataGridViewColumn":
						e.Value = ParametriList[e.RowIndex].IDGrupaParametra;
						break;
					case "uxParamDataGridViewColumn":
						e.Value = ParametriList[e.RowIndex].Param;
						break;
					case "uxVrijednostDataGridViewColumn":
						e.Value = ParametriList[e.RowIndex].Vrijednost;
						break;
					case "uxIDDataGridViewColumn":
						e.Value = ParametriList[e.RowIndex].ID;
						break;
				default:
				break;
			}
		}
		
		/// <summary>
		/// Handles the CellValuePushed event of the uxParametriDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewCellValueEventArgs"/> instance containing the event data.</param>
		protected void OnParametriDataGridViewCellValuePushed(object sender, System.Windows.Forms.DataGridViewCellValueEventArgs e)
		{
			switch(uxParametriDataGridView.Columns[e.ColumnIndex].Name)
			{
					case "uxIDGrupaParametraDataGridViewColumn":
						ParametriList[e.RowIndex].IDGrupaParametra = (System.Int32)e.Value;
						break;
					case "uxParamDataGridViewColumn":
						ParametriList[e.RowIndex].Param = (System.String)e.Value;
						break;
					case "uxVrijednostDataGridViewColumn":
						ParametriList[e.RowIndex].Vrijednost = (System.String)e.Value;
						break;
					case "uxIDDataGridViewColumn":
						ParametriList[e.RowIndex].ID = (System.Int32)e.Value;
						break;
				default:
				break;
			}
		}
	}
}
